package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserInfoMapper {
//    @Select("select * from userinfo")
//    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where id = #{id}")
    List<UserInfo> getUserInfoId(Integer id);

    @Select("select * from userinfo where id = ${id}")
    List<UserInfo> getUserInfoId2(Integer id);

    @Select("select * from userinfo where username = #{username}")
    List<UserInfo> getUserInfoName(String username);

    @Select("select * from userinfo where username = '${username}'")
    List<UserInfo> getUserInfoName2(String username);

    @Select("select * from userinfo where id = #{id} and age = #{age}")
    List<UserInfo> getUserInfoIdAndAge(Integer id, Integer age);

//    @Select("select * from userinfo where id = #{param1} and age = #{param2}")
//    List<UserInfo> getUserInfoIdAndAge(Integer id, Integer age);

    @Select("select * from userinfo where username = #{userName} and id = #{id}")
    List<UserInfo> getUserInfoNameAndId(@Param("userName")String name, @Param("id") Integer id);

//    @Options(useGeneratedKeys = true, keyProperty = "id")
//    @Insert("insert  into userinfo(id, username, password, age) values (#{id}, #{userName}, #{password}, #{age})")
//    Integer insert(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert  into userinfo(id, username, password, age) " +
            "values (#{user.id}, #{user.username}, #{user.password}, #{user.age})")
    Integer insert(@Param("user") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password}, " +
            "age = #{age}, gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

//    //起别名
//    @Select("select id, username, password, age, gender, phone, " +
//            "delete_flag as deleteFlag, create_time as createTime, " +
//            "update_time as updateTime from userinfo")
//    List<UserInfo> getUserInfoAll();

//    //结果映射
//    @Results(id = "resultMap", value = {
//            @Result(column = "delete_flag", property = "deleteFlag"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//    })
//    @Select("select * from userinfo")
//    List<UserInfo> getUserInfoAll();
//
//    @ResultMap("resultMap")
//    @Select("select * from userinfo")
//    List<UserInfo> getUserInfoAll2();


    //驼峰自动转换
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo order by id ${sort}")
    List<UserInfo> getUserInfoBySort(String sort);

    @Select("select * from userinfo where username like concat('%', #{username}, '%')")
    List<UserInfo> getUserInfoByLike1(String username);
    @Select("select * from userinfo where username like '%${username}%'")
    List<UserInfo> getUserInfoByLike2(String username);
}
